package p5sameWord;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import p2tokenstream.TokenAttributeUtils;

import java.io.IOException;

/**
 * 同义词搜索，（敏感词）
 */
public class TestSameWords {
    /**
     * 模拟出同义词，这个是直接调用底层方法，分词
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        Analyzer analyzer=new MySameWordAnalyzer();
        TokenAttributeUtils.displayAllTokenInfo("i am come from china henan province xingyang city",analyzer);
    }

    @Test
    public void test2() throws IOException {
        String txt="i am come from china henan province xingyang city";
        //创建索引
        Analyzer analyzer=new MySameWordAnalyzer();
        Directory directory=new RAMDirectory();
        IndexWriter writer=new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35,analyzer));
        writer.deleteAll();
        Document doc=new Document();
        doc.add(new Field("content",txt,Field.Store.YES,Field.Index.ANALYZED));
        writer.addDocument(doc);
        writer.close();
        //查询索引
        IndexSearcher searcher=new IndexSearcher(IndexReader.open(directory));
        TopDocs topDocs=searcher.search(new TermQuery(new Term("content","my")),10);
        ScoreDoc [] scoreDocs=topDocs.scoreDocs;
        for(ScoreDoc sd:scoreDocs){
           Document doc2= searcher.doc(sd.doc);
            System.out.println(doc2.get("content"));
        }
    }
}
